package com.epalmpay.job;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.epalmpay.exception.ApplicationException;
import com.epalmpay.enumdef.CommonEnum.RunStatus;
import com.epalmpay.enumdef.CommonEnum.TaskType;
import com.epalmpay.service.jobservice.ITradeStatisService;
import com.epalmpay.service.operator.ISysLogService;
import com.epalmpay.util.DateUtil;
import com.epalmpay.util.Logger;

@Component
public class GoodsMerchantSettedTask {

	@Resource
	private ISysLogService sysLogService;
	@Resource
	private ITradeStatisService tradeStatisService;

	
	protected Logger logger = Logger.getLogger(this.getClass());	
	
	/** 逻辑：如果需要直接使用的线下 需要结算的 
	 * 
	 * 门店 商品需要结算的 将钱打入商户余额
	 * 每天凌晨5点00分执行一次
	 */
	@Scheduled(cron="0 00 4 * * ?")
	//@Scheduled(cron="0 0 09 * * ?")
	public void  run(){

		//统计日期
		Date statisDate = null;
		StringBuffer sb = new StringBuffer(); 
		try {			
			//取得昨天日期
			//statisDate = DateUtil.getAfterDay(-1);
			statisDate=new Date();
			 tradeStatisService.finishGoodsMerchantSetted(statisDate);
			logger.info(sb.append("计划任务:")
					.append(TaskType.GoodsMerchantSetted.getName())
					.append("统计日期：").append(DateUtil.fomatDate(statisDate, DateUtil.sdfDays)).append("执行成功！"));
		
		} catch (ApplicationException ae) { 
			logger.info(sb.append("计划任务:")
					.append(TaskType.GoodsMerchantSetted.getName())
					.append("统计日期：").append(DateUtil.fomatDate(statisDate, DateUtil.sdfDays)).append("执行结果：").append(ae.errMsg));				
		}		
		 catch (Exception e) { 
			 try{
				sysLogService.recordSysTaskLog(TaskType.GoodsMerchantSetted.getType(),
						statisDate, RunStatus.Fail.getType());
			 }catch(Exception sube){
					logger.info(sb.append("计划任务:")
							.append(TaskType.CouponMerchantSetted.getName())
							.append("统计日期：").append(DateUtil.fomatDate(statisDate, DateUtil.sdfDays)).append("执行失败！"));				 
				 logger.error(sube);
			 }
			e.printStackTrace();
			logger.info(sb.append("计划任务:")
					.append(TaskType.GoodsMerchantSetted.getName())
					.append("统计日期：").append(DateUtil.fomatDate(statisDate, DateUtil.sdfDays)).append("执行失败！"));			
			logger.error(e);
		}
	}
}
